#include "stm32f0xx.h"
#include "stm32f0_discovery.h"
#include <stdio.h>

uint32_t msecTick = 0;

/* Configuring GPIO ports */

void GPIO_Config() {
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
}

/* USART configuration */


void USART_Config() {
	USART_InitTypeDef USART_InitStructure;

	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(USART1, &USART_InitStructure);

	USART_Cmd(USART1, ENABLE);
}

void Board_Init() {
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

	GPIO_Config();
	USART_Config();
  
	STM_EVAL_LEDInit(LED3);
	STM_EVAL_LEDInit(LED4);
  // Configure SysTick for 1-msec interval 
  if (SysTick_Config(SystemCoreClock / 1000)){
		while (1);
	}
}
void TimingDelay_Decrement() {
	if (msecTick > 0)
		msecTick--;
}

void DelayWait(uint32_t tick) {
  	msecTick = tick;
  	while (msecTick != 0);
}

int fputc(int ch, FILE *stream) {
  USART_SendData(USART1, (uint16_t)ch);
  while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET);
  return ch;
}

int fgetc(FILE *stream) {
	while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != SET);
  return USART_ReceiveData(USART1);
}
/* Here I have corded for the command interpriter.*/

int main() {
	Board_Init();
	while(1) {
		int8_t buffer[20];
		scanf("%s", buffer);
		if (buffer[0]=='L'&&buffer[1]=='3'){
			STM_EVAL_LEDOn(LED3);
			STM_EVAL_LEDOff(LED4);
		}
		if (buffer[0]=='L'&&buffer[1]=='4'){
			STM_EVAL_LEDOff(LED3);
			STM_EVAL_LEDOn(LED4);
		}
		printf("%s\n", buffer);
	}
}
